const app = getApp()
// var template = require('../../template/template.js');
var ea = require('../../utils/each.js');
Page({
  data: {
    checkedddd: false,
    getjiangpinUrl: app.data.url +'/mapi/index/getjiangpin',
    deljiangpinUrl: app.data.url +'/mapi/index/delwinner',
    jiesuanUrl: app.data.url +'/mapi/index/jiesuan',
    getaddress:app.data.url+'/mapi/newfenxiao/getaddress',
    bijiaourl: app.data.url + '/mapi/newfenxiao/bijiao',
    shuju:[],
    page:1,
    yijiaoyajin:0,
    haigei:0,
    zuixiao:0,
    zong:0,
    keyi:true,
    address:{},
  },

  jianche: function () {
    var shuju = this.data.shuju;
    var checkedddd = this.data.checkedddd;
    var quan = true;
    if(shuju.length==0){
      quan = false;
    }
    ea.each(shuju,function(i,v){
      if(!v.checked){
        quan = false;
      }
    })
    if(quan){
      this.setData({ checkedddd:true});
    }else{
      this.setData({ checkedddd: false });
    }
  },
  kouyajinhezhifu:function(){
    var shuju = this.data.shuju;
    var yijiaoyajin = 0;
    var haigei = 0;
    var shifouyouxuan = false;
    ea.each(shuju,function(i,v){
      if(v.checked){
        shifouyouxuan = true;
        // yijiaoyajin = yijiaoyajin + parseFloat(v.yajin);
        if (v.xian_price!=='免费'){
          yijiaoyajin = yijiaoyajin + parseFloat(v.yajin);
          haigei = haigei + parseFloat(v.xian_price) - parseFloat(v.yajin);
        }
      }
    })
    if (shifouyouxuan){
      var address = this.data.address;
      if (address.lingjia) {
        haigei = haigei + parseFloat(address.lingjia.qian);
      }
    }
    haigei = haigei.toFixed(2)
    // console.log(haigei);
    this.setData({ yijiaoyajin: yijiaoyajin})
    this.setData({ haigei: haigei});
  },
//点击全选
  qiehuan: function () {
    var checkedddd = this.data.checkedddd;
    var shuju = this.data.shuju;
    var newshuju = [];
    if (checkedddd){
      checkedddd = false;
      ea.each(shuju, function (i, v) {
        v.checked = false;
        newshuju.push(v);
      })
    }else{
      checkedddd = true;
      ea.each(shuju,function(i,v){
        v.checked = true;
        newshuju.push(v);
      })
    }
    this.setData({ shuju: newshuju });
    this.setData({ checkedddd:checkedddd})
    this.kouyajinhezhifu();
  },

//点击单选
  dqiehuan: function (e) {
    // console.log(e);
    var index = e.currentTarget.dataset.index;
    var shuju = this.data.shuju;
    console.log(shuju[index].checked);
    if(shuju[index].checked){
      shuju[index].checked = false;
    }else{
      shuju[index].checked = true;
    }
    this.setData({shuju});
    this.jianche();
    this.kouyajinhezhifu();
  },
  
  //跳转到订单详情
  chanpinxiangqing:function(e){
    // console.log(e);
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/dingdanxiangqing/dingdanxiangqing?id='+id,
    })
  },
  //
  onLoad:function(){
    this.tianjaishuju();
  },
  //分享函数
  onShareAppMessage: function () {
    var user = wx.getStorageSync('user');
    return {
      path: '/pages/index/index?pid' + user.id,
    };
  }
  //下拉刷新
  , onPullDownRefresh: function () {
    this.setData({page:1});
    this.setData({shuju:[]});
    this.onLoad();
    wx.stopPullDownRefresh({
      success: function () {
        console.log('返回了')
      }
    });
  },
  //触底加载
  onReachBottom:function(){
    var page = this.data.page;
    if(page){
      this.setData({page:page+1});
      this.tianjaishuju();
    }else{
      wx.showToast({
        title: '没有更多数据了',
        icon:'none'
      })
    }
  },
  //获取数据
  tianjaishuju:function(){
    var that = this;
    var page = that.data.page;
    if(page>0){
      var user = wx.getStorageSync('user');
      wx.showLoading({
        title: '加载中',
      })
      wx.request({
        url: that.data.getjiangpinUrl,
        method: 'post',
        data: { token: user.token ,page:page},
        success: function (res) {
          var data = res.data;
          // console.log(data);
          // return;
          if (data.code == 200) {
            console.log(data);
            // return;
            that.setData({zong:data.data.data.total})
            that.setData({ zuixiao: data.data.guizhe.dir })
            var data = data.data.data.data;
            // console.log(data);
            // that.setData({ zuixiao: data.data.data.guizhe.dir})
            if(data.length<10){
              that.setData({page:0});
            }
            var shuju = that.data.shuju;
            ea.each(data,function(i,v){
              v.checked = false;
              shuju.push(v);
            })
            console.log(shuju);
            that.setData({shuju:shuju});
            that.jianche();
            that.kouyajinhezhifu();
          } else {
            wx.showToast({
              title: data.msg,
              icon: 'none'
            })
          }
          wx.hideLoading();
        }
      })
    }else{
      wx.showToast({
        title: '没有更多数据了',
        icon:'none'
      })
    }
  },
  //删除
  shanchuzhexi:function(){
    var that = this;
    var shuju = that.data.shuju;
    var shujuid = [];
    ea.each(shuju,function(i,v){
      if(v.checked){
        shujuid.push(v.id);
      }
    });
    // console.log(shujuid)
    // console.log(shujuid.indexOf(25))
    // return;
    wx.showModal({
      title: '提示',
      content: '这些东西删除了，就再也找不回来了哦！！',
      success:function(e){
        if(e.confirm){
          var user = wx.getStorageSync('user');
          wx.showLoading({
            title: '操作中',
          })
          wx.request({
            url: that.data.deljiangpinUrl,
            method:'post',
            data:{token:user.token,id:shujuid},
            success:function(res){
              var data = res.data;
              if(data.code==200){
                wx.showToast({
                  title: '删除成功',
                });
            var newshuju = [];
            console.log(shujuid);
            ea.each(shuju,function(i,v){
              if (shujuid.indexOf(v.id)==-1){
                newshuju.push(v);
              }
            })
            // console.log(newshuju);
            if(newshuju.length==0){
              that.setData({shuju:[]});
              that.setData({page:1});
            }
                that.setData({ shuju: newshuju});
                that.jianche();
                that.kouyajinhezhifu()
              }else{
                wx.showToast({
                  title: data.msg,
                  icon:'none'
                })
              }
              wx.hideLoading();
            }
          })
        }
      }
    })
  },
  //结算接口
  jaesuan:function(e){
    var formid = e.detail.formId;
    var rmb  = this.data.haigei;
    var address = this.data.address;
    if(!address.id){
      wx.showToast({
        title: '请添加地址',
        icon:'none'
      })
      return;
    }
    var that = this;
    var user = wx.getStorageSync('user');
    // console.log(rmb);
    wx.request({
      url: that.data.bijiaourl,
      method:'post',
      data:{token:user.token,rmb:rmb},
      success:function(res){
        var data  = res.data;
        console.log(data);
        // return;
        if(data.code==200){
          that.zhenjiesuan(formid);
        }else{
          var newshuju =data.data;
          console.log(newshuju);
          wx.requestPayment({
            timeStamp: newshuju.timeStamp,
            nonceStr: newshuju.nonceStr,
            package: newshuju.package,
            signType: newshuju.signType,
            paySign: newshuju.paySign,
            success:function(){
              setTimeout(function(){
                that.zhenjiesuan(formid);
              },500)
            }
          })
        }
      }
    })

    // this.zhenjiesuan(formid);
  },
  onShow:function(){
    var nowaddress = wx.getStorageSync('nowaddress');
    var user = wx.getStorageSync('user');
    var tiaojian = {token:user.token};
    var that = this;
    if(nowaddress){
      console.log(nowaddress);
      tiaojian.addressid = nowaddress;
      wx.setStorageSync('nowaddress', '')
    }else{
      if (this.data.address) {
        tiaojian.addressid = this.data.address.id;
      }
    }
    console.log(tiaojian);
    wx.request({
      url: that.data.getaddress,
      method:'post',
      data:tiaojian,
      success:function(res){
        var data = res.data;
        // console.log(data);
        if(data.code==200){
          var address = data.data;
          // console.log(address);
          if (address){
            that.setData({ address: address});
          }else{
            that.setData({ address: {} });
            wx.showToast({
              title: "你还没有添加地址",
              icon: 'none'
            })
          }
          that.kouyajinhezhifu();
        }else{
          that.setData({address:{}});
          wx.showToast({
            title: data.msg,
            icon:'none'
          })
        }

      }
    })
  },
  xuanzdiz:function(){
    var address = this.data.address;
    if(address.id){
      wx.navigateTo({
        url: '/pages/xuanzeaddress/xuanzeaddress?id=' + address.id,
      })
    }else{
      wx.navigateTo({
        url: '/pages/bianjidizhi/bianjidizhi',
      })
    }
  },
  zhenjiesuan:function(formid){
    var that = this;
    var keyi = this.data.keyi;
    if (!keyi) {
      return;
    }
    this.setData({ keyi: false });
    var zhifu = this.data.haigei;
    var shuju = this.data.shuju;
    var zuixiao = this.data.zuixiao;
    var shujuid = [];
    ea.each(shuju, function (i, v) {
      if (v.checked) {
        shujuid.push(v.id);
      }
    });
    // console.log(zhifu);
    if (shujuid.length < zuixiao) {
      wx.showToast({
        title: '对不起,奖品必须达到' + zuixiao + '个及以上才能发货！！',
        icon: 'none'
      });
      that.setData({ keyi: true });
      return;
    }
    var user = wx.getStorageSync('user');
    // console.log(zhifu);
    // console.log(shujuid);
    // return;
    console.log(formid);
    var address = that.data.address;
    var tiaojian = { token: user.token, rmb: zhifu, id: shujuid, formid: formid };
    if (address) {
      tiaojian.addressid = address.id;
    }
    // return;
    wx.showModal({
      title: '提示',
      content: '你确定去结算?',
      // cancelText:text2,
      // confirmText: text1,
      success: function (e) {
        console.log(e);
        if (e.confirm) {
          wx.request({
            url: that.data.jiesuanUrl,
            method: 'post',
            data: tiaojian,
            success: function (res) {
              console.log(res);
              // that.setData({ keyi: true });
              // return;
              var data = res.data;
              console.log(data);
              // return;
              if (data.code == 209) {
                wx.showToast({
                  title: data.msg,
                  icon: 'none'
                })
                that.setData({ keyi: true });
                setTimeout(function () {
                  wx.navigateTo({
                    url: '/pages/chongzhi/chongzhi',
                  })
                }, 1000)
              } else if (data.code == 200) {
                wx.showToast({
                  title: '兑换成功',
                });
                setTimeout(function () {
                  that.setData({ shuju: [] });
                  that.setData({ page: 1 });
                  that.setData({ keyi: true });
                  that.onLoad();
                }, 1000)
                console.log(data.data);
              } else if (data.code == 210) {
                wx.showToast({
                  title: data.msg,
                  icon: 'none'
                })
                setTimeout(function () {
                  that.setData({ keyi: true });
                  wx.navigateTo({
                    url: '/pages/suohuodz/suohuodz',
                  })
                }, 1000)
              } else {
                that.setData({ keyi: true });
                wx.showToast({
                  title: data.msg,
                  icon: 'none'
                })
              }
            }
          })
        } else {
          that.setData({ keyi: true });
        }
      }
    })
  },
  onShareAppMessage: function () {
    var user = wx.getStorageSync('user');
    var titles = wx.getStorageSync('titles');
    return {
      title: titles,
      path: '/pages/index/index?scene' + user.id,
    };
  }
})

